package org.example.demo_xiaowuz.service;

import org.example.demo_xiaowuz.enums.StatusEnum;
import org.example.demo_xiaowuz.mapper.BookMapper;
import org.example.demo_xiaowuz.model.BookInfo;
import org.example.demo_xiaowuz.model.PageResult;
import org.example.demo_xiaowuz.model.RequestPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BookService {
    @Autowired
    BookMapper bookMapper;


    public Integer addBook(BookInfo bookInfo) {
        return bookMapper.addBook(bookInfo);
    }

    public PageResult<BookInfo> getListByPage(RequestPage requestPage) {
        Integer count=bookMapper.count();
        List<BookInfo> list=bookMapper.getListByPage(requestPage.getOffset()
                ,requestPage.getPageSize());
        for (BookInfo bookInfo:list) {
            bookInfo.setStateCN(StatusEnum.getNameByCode(bookInfo.getStatus()).getName());
        }
        return new PageResult<>(list,count,requestPage);
    }

    public BookInfo getBookInfo(Integer bookId) {
        return bookMapper.getBookInfo(bookId);
    }

    public Integer updateBook(BookInfo bookInfo) {
        return bookMapper.updateBook(bookInfo);
    }

    public Integer batchDelete(List<Integer> ids) {
        return bookMapper.batchDelete(ids);
    }
}
